package org.itboys.core.service.mysql.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.coupon.SundayCouponMapper;
import org.itboys.core.dto.product.SundaySkuDto;
import org.itboys.core.entity.mongo.SundayCouponSKU;
import org.itboys.core.entity.mongo.SundayCouponSubstract;
import org.itboys.core.entity.mongo.SundayCouponUserArea;
import org.itboys.core.entity.mysql.SundayCoupon;
import org.itboys.core.service.mongo.web.SundayCouponSKUService;
import org.itboys.core.service.mongo.web.SundayCouponSubstractService;
import org.itboys.core.service.mongo.web.SundayCouponUserAreaService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/28
 * @Description：优惠券
 */
@Service
public class SundayCouponService extends BaseMySqlService<SundayCoupon>{
    @Autowired
    private SundayCouponMapper couponMapper;
    @Autowired
    private SundayCouponSKUService couponSKUService;
    @Autowired
    private SundayCouponUserAreaService couponUserAreaService;
    @Autowired
    private SundayCouponSubstractService couponSubstractService;
    @Autowired
    private SundaySkuService skuService;
    public void save(Long id, SundayCoupon coupon, SundayCouponSubstract couponSubstracts,
                     List<Integer> userAreas, List<Long> SKUs){

        Long couponId;
        if(LongUtil.isNotNull(id)){
            coupon.setId(id);
            couponId=id;
            couponMapper.update(coupon);
        }else {
            couponMapper.insert(coupon);
            couponId=coupon.getId();
        }
        List<SundayCouponUserArea> couponUserAreas=new ArrayList<>();
        List<SundayCouponSKU> couponSKUS=new ArrayList<>();
        for (Integer userType:userAreas
                ) {
            SundayCouponUserArea couponUserArea=new SundayCouponUserArea( couponId,  userType);
            couponUserAreas.add(couponUserArea);
        }
        for (Long skuId:SKUs
                ) {
            SundayCouponSKU couponSKU=new SundayCouponSKU(couponId,skuId);
            couponSKUS.add(couponSKU);
        }
        couponSubstracts.setCouponId(couponId);
        //更新该优惠券下所有的sku范围
        Map<String,Object> param=new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("couponId",coupon.getId());
        couponSKUService.delete(param);
        couponSKUService.batchSave(couponSKUS);
        //更新所有的用户范围
        couponUserAreaService.delete(param);
        couponUserAreaService.batchSave(couponUserAreas);
        //更新所有的满减规则
        couponSubstractService.delete(param);
        couponSubstractService.save(couponSubstracts);
    }

    public SundayCoupon findOne(Long couponId){
        if(LongUtil.isNotZreo(couponId)){
            SundayCoupon coupon = couponMapper.find(couponId);
            Map<String,Object> param=new HashMap<>();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("couponId",couponId);
            List<SundayCouponSKU> couponSKUs= couponSKUService.list(param);
            List<SundayCouponUserArea> couponUserAreas = couponUserAreaService.list(param);
            List<SundayCouponSubstract> couponSubstracts = couponSubstractService.list(param);
            if(ListUtil.isNotNull(couponSubstracts)){
                coupon.setSubstract(couponSubstracts.get(0));
            }
            List<Integer> userTypes=new ArrayList<>();
            List<Long> skuIds=new ArrayList<>();
            for(SundayCouponUserArea userArea:couponUserAreas){
                userTypes.add(userArea.getUserType());
            }
            for(SundayCouponSKU sku:couponSKUs){
                skuIds.add(sku.getSkuId());
            }
            param.clear();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("ids",skuIds );
            List<SundaySkuDto> sundaySkuDtos = skuService.packageDtoMuilt(skuService.select(param));
            for (SundaySkuDto dto:sundaySkuDtos
                 ) {
                for (SundayCouponSKU couponSKu:couponSKUs
                     ) {
                    if(couponSKu.getSkuId().equals(dto.getId())){
                        couponSKu.setProductName(dto.getProductName());
                        couponSKu.setSkuNumber(dto.getNumber());
                    }
                }

            }
            coupon.setCouponSKUS(couponSKUs);
            coupon.setUserTypes(userTypes);
            return coupon;
        }
        return null;
    }

    public PageResult<SundayCoupon> select(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request,"name");
        return super.selectPage(param);
    }
}
